package com.neuedu.service.impl;

import com.neuedu.dao.AddBuildingsNumberDao;
import com.neuedu.dao.BuildingsDao;
import com.neuedu.dao.impl.AddBuildingsNumberDaoImpl;
import com.neuedu.dao.impl.BuildingsDaoImpl;
import com.neuedu.model.AddBuildingsNumber;
import com.neuedu.model.Buildings;
import com.neuedu.service.BuildingsService;
import com.neuedu.vo.PageVO;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class BuildingsServiceImpl implements BuildingsService {
    private BuildingsDao buildingsDao=new BuildingsDaoImpl();
    private AddBuildingsNumberDao addBuildingsNumberDao=new AddBuildingsNumberDaoImpl();
    @Override
    public PageVO<Buildings> selectPage(String minDate, String maxDate, String likeName, Integer pageNo, Integer pageSize) {
        //计算查询时偏移量 例如第2页 每页10条记录 那么查询从第10条开始(2-1)*10 = 10
        Integer limitParams1=(pageNo-1)*pageSize;
        //pageSize 表示每页显示多少条记录
        Integer limitParams2=pageSize;
        List<Buildings> buildings = buildingsDao.selectPage(minDate, maxDate, likeName, limitParams1, limitParams2);
        //创建了一个 PageVO<XiaoQu> 对象，用来封装分页查询的结果
        PageVO<Buildings> pageVO=new PageVO<>();
        //设置当前页码，表示用户查询的是第几页
        pageVO.setPageNo(pageNo);
        //设置每页显示的记录数，表示每页会展示多少条数据
        pageVO.setPageSize(pageSize);
        //将查询得到的 Buildings 列表数据设置到 PageVO 对象中，表示当前页包含的具体数据。
        pageVO.setData(buildings);
        //还差  pageCount页的总数  itemCount数据的总数
        Integer itemCount=buildingsDao.count(minDate, maxDate, likeName);
        Integer pageCount=((itemCount%pageSize)==0)?(itemCount/pageSize):(itemCount/pageSize+1);
        pageVO.setItemCount(itemCount);
        pageVO.setPageCount(pageCount);
        return pageVO;
    }

    @Override
    public int addBuildings(Buildings buildings) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);

        // 获取 AddBuildingsNumber 对象
        AddBuildingsNumber addBuildingsNumber = addBuildingsNumberDao.select();
        String number = "CJ";
        number = number + format.replace("-", "");

        // 如果是同一天的编号
        if (addBuildingsNumber.getNumberTime().equals(format)) {
            if (addBuildingsNumber.getNumber() < 10) {
                number = number + "00" + addBuildingsNumber.getNumber();
            } else if (addBuildingsNumber.getNumber() < 100) {
                number = number + "0" + addBuildingsNumber.getNumber();
            } else {
                number = number + addBuildingsNumber.getNumber();
            }
            addBuildingsNumber.setNumber(addBuildingsNumber.getNumber() + 1);
            // 使用 DAO 层更新
            addBuildingsNumberDao.update(addBuildingsNumber);
        } else {
            // 如果是新的一天，重置编号
            addBuildingsNumber.setNumberTime(format);
            addBuildingsNumber.setNumber(1);
            // 使用 DAO 层更新
            addBuildingsNumberDao.update(addBuildingsNumber);
            number = number + "001";
        }

        // 设置编号
        buildings.setBianhao(number);

        // 设置创建时间
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        format = sdf.format(date);
        buildings.setChuangjianshijian(format);

        // 插入 Buildings 数据
        return buildingsDao.insertBuildings(buildings);
    }


    @Override
    public int delBuildings(Integer[] ids) {
        return buildingsDao.deleteBulidings(ids);
    }

    @Override
    public Buildings queryBuildings(Integer id) {
        return buildingsDao.selectBulidingsById(id);
    }


    @Override
    public int saveBuildings(Buildings buildings) {
        return buildingsDao.updateBulidings(buildings);
    }

}
